﻿#include "SectionWidget.h"
#include "ui_SectionWidget.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAbstractSection.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIFrame/MainWindow.h"
#include "SectionInfoDialog.h"
#include <QButtonGroup>
#include <QDebug>
#include <QMessageBox>
#include <QWidget>

#include "GUIFrame/PropertyWidget.h"
#include "FITK_Interface/FITKInterfaceFlowOF/FITKOFPhysicsData.h"

namespace GUI
{
    SectionWidget::SectionWidget(EventOper::ParaWidgetInterfaceOperator* oper, QWidget* parent) :
        GUIWidgetBase(parent), _oper(oper)
    {
        _ui = new Ui::SectionWidget();
        _ui->setupUi(this);
        initDialog();
    }

    SectionWidget::~SectionWidget()
    {
        delete _ui;
    }

    void SectionWidget::initDialog()
    {
        //截面类型添加体、单元、梁
        QButtonGroup* categoryGroup = new QButtonGroup(this);
        categoryGroup->addButton(_ui->rb_solid, 1);
        categoryGroup->addButton(_ui->rb_shell, 2);
        categoryGroup->addButton(_ui->rb_beam, 3);
        //截面类型修改信号连接
        connect(categoryGroup, SIGNAL(buttonToggled(int, bool)), this, SLOT(categoryGroupToggledSlot(int, bool)));

        _ui->rb_solid->setChecked(true);

        //获取界面管理器
        Interface::FITKOFPhysicsData* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Interface::FITKOFPhysicsData>();
        if (physicsData == nullptr)return;
        Interface::FITKAbstractSectionManager* sectionManager =physicsData->getSectionManager();
        if (sectionManager == nullptr)return;

        //设置默认截面名称
        QString name = QString("Section-%1").arg(sectionManager->getDataCount() + 1);
        _ui->le_name->setText(name);

        _ui->listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    }

    void SectionWidget::categoryGroupToggledSlot(int btn, bool checked)
    {
        _ui->listWidget->clear();
        if (!checked) return;
        //界面类型切换，list表数据更新
        switch (btn)
        {
        case 1:
        case 2:
            _ui->listWidget->addItem(QObject::tr("Homogeneous"));
            break;
        case 3:
        {
            _ui->listWidget->addItem(QObject::tr("Beam"));
            break;
        }
        }
        if (_ui->listWidget->count() != 0)
        {
            _ui->listWidget->setItemSelected(_ui->listWidget->item(0), true);
        }
    }

    void SectionWidget::on_pb_continue_clicked()
    {
        // 数据安全校验
        QString name = _ui->le_name->text().trimmed();
        if (name.isEmpty())
        {
            QMessageBox::critical(this, tr("Error Information"), tr("Name can not be empty!"));
            return;
        }
        //获取界面管理器
        //获取界面管理器
        Interface::FITKOFPhysicsData* physicsData = FITKAPP->getGlobalData()->getPhysicsData<Interface::FITKOFPhysicsData>();
        if (physicsData == nullptr)return;
        Interface::FITKAbstractSectionManager* sectionManager =physicsData->getSectionManager();
        if (sectionManager == nullptr)return;
        //判断名称是否存在
        if (sectionManager->getDataByName(name))
        {
            QMessageBox::critical(this, tr("Error Information"), tr("Section with the same name already exists!"));
            return;
        }
        QList< QListWidgetItem* > selectedTypes = _ui->listWidget->selectedItems();
        if (selectedTypes.size() != 1)
        {
            QMessageBox::critical(this, tr("Error Information"), tr("No selected type!"));
            return;
        }

        //获取section的类型
        Interface::FITKAbaAbastractSection::FITKAbaSectionType type =
            Interface::FITKAbaAbastractSection::FITKAbaSectionType::None;
        if (_ui->rb_solid->isChecked())
        {
            type = Interface::FITKAbaAbastractSection::FITKAbaSectionType::SolidHomogeneous;
        }
        else if (_ui->rb_shell->isChecked())
        {
            type = Interface::FITKAbaAbastractSection::FITKAbaSectionType::ShellHomogeneous;
        }
        else if (_ui->rb_beam->isChecked())
        {
            type = Interface::FITKAbaAbastractSection::FITKAbaSectionType::Beam;
        }
        //界面信息对话框创建
        GUI::SectionInfoDialog* dlg = new GUI::SectionInfoDialog(_mainWin, _oper, name, type);
        dlg->show();

        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        propertyWidget->init();
        //this->accept();
    }

    void SectionWidget::on_pb_cancel_clicked()
    {
        GUI::MainWindow* mainWindow = dynamic_cast<GUI::MainWindow*>(FITKAPP->getGlobalData()->getMainWindow());
        if (mainWindow == nullptr)return;
        GUI::PropertyWidget* propertyWidget = mainWindow->getPropertyWidget();
        if (propertyWidget == nullptr)return;

        propertyWidget->init();
        //this->reject();
    }
}  // namespace GUI
